﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;

namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucDatLichTruc : UserControl
    {
        String NVID;
        String LT;
        String ID_Co;
        public ucDatLichTruc(string IDNV)
        {

            InitializeComponent();
            this.NVID = IDNV;
            ShowData();
            ShowLoaiTruc();
            pnpChiTietLichTruc.Visible = false;

        }

        void ShowData()
        {
            NhanVienBUO nv = new NhanVienBUO();
            int makhoa = nv.GetMaKhoa(int.Parse(NVID));
            KhoaBUO khoa = new KhoaBUO();

            cbNhanVien.DataSource = nv.GetListNVByKhoa(makhoa).Select(p => new { ID = p.NhanVienID, Ten = p.FirstName + " " + p.LastName }).ToList();
            cbNhanVien.DisplayMember = "TEN";
            cbNhanVien.ValueMember = "ID";
            lblmess.Text = "Bạn đang cài lịch trực cho Khoa " + khoa.GetTenKhoa(makhoa);



        }

        void ShowLoaiTruc()
        {
            ComboboxItem item1 = new ComboboxItem("Ca sáng", 1);
            ComboboxItem item2 = new ComboboxItem("Ca chiều", 2);
            ComboboxItem item3 = new ComboboxItem("Ca đêm", 3);

            List<ComboboxItem> list = new List<ComboboxItem>();
            list.Add(item1);
            list.Add(item2);
            list.Add(item3);

            cbCaTruc.DataSource = list;
            cbCaTruc.DisplayMember = "Text";
            cbCaTruc.ValueMember = "Text";
            dtpDatlich.CustomFormat = "dd/MM/yyyy";
            dtpDatlich.Format = DateTimePickerFormat.Short;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            tbl_ChiTietLichTruc chitiet = new tbl_ChiTietLichTruc();
            ChiTietLichTrucBUO ct = new ChiTietLichTrucBUO();
            chitiet.NhanVienID = int.Parse(cbNhanVien.SelectedValue.ToString());
            chitiet.CaTrucID = cbCaTruc.SelectedValue.ToString();
            chitiet.LichTrucID = int.Parse(LT);
            if (ct.Insert(chitiet))
            {
                MessageBox.Show("Thành công");

            }
            else
            {
                MessageBox.Show("Thất bại");
            }

            ShowGrid(int.Parse(LT));

        }


        void ShowGrid(int idLichTruc)
        {
            HospitalDBDataContext context = new HospitalDBDataContext();

            var t = context.tbl_ChiTietLichTrucs.Join(context.tbl_LichTrucs, c => c.LichTrucID, d => d.LichTrucID,
                  (c, d) => new
                  {
                      ChitietLichID = c.ChiTietLichID,
                      NhanVienID = c.NhanVienID,
                      CaTruc = c.CaTrucID,
                      LichTrucID = d.LichTrucID,

                  }
                  ).Join(context.tbl_NhanViens, c => c.NhanVienID, d => d.NhanVienID, (c, d) => new
                  {
                      SoSoNV = d.NhanVienID,
                      HoTen = d.FirstName + " " + d.LastName,
                      Adress = d.Address,
                      Tel = d.Tel,
                      CaTruc = c.CaTruc,
                      ViTriID = d.Position,
                      LichTruc = c.LichTrucID,
                      ChitietLichID = c.ChitietLichID



                  }).Join(context.tbl_Positions, c => c.ViTriID, d => d.PositionId, (c, d) => new
                  {
                      ChitietLichID = c.ChitietLichID,
                      SoSoNV = c.SoSoNV,
                      HoTen = c.HoTen,
                      Adress = c.Adress,
                      Tel = c.Tel,
                      CaTruc = c.CaTruc,
                      ViTri = d.PositionName,
                      LichTrucID = c.LichTruc


                  }).Where(k => k.LichTrucID == idLichTruc);
            dataGridView1.DataSource = t;



        }
        private void button2_Click(object sender, EventArgs e)
        {
            NhanVienBUO nv = new NhanVienBUO();
            LichTrucBUO lt = new LichTrucBUO();
            tbl_LichTruc obj = new tbl_LichTruc();
            obj.KhoaPhongID = nv.GetMaKhoa(int.Parse(NVID));
            obj.NgayDatLich = dtpDatlich.Text;
            obj.ChargeNureID = int.Parse(NVID);
            if (lt.Insert(obj))
            {
                MessageBox.Show(" thành công");
                LT = lt.GetMaLichTruc(int.Parse(obj.KhoaPhongID.ToString()), dtpDatlich.Text).ToString();
                pnpChiTietLichTruc.Visible = true;
            }
            else
            {
                MessageBox.Show("Thất công");
                pnpChiTietLichTruc.Visible = false;
            }
            ShowGrid(int.Parse(LT));
        }



        void CheckLichTruc()
        {
            NhanVienBUO nv = new NhanVienBUO();
            LichTrucBUO lt = new LichTrucBUO();
            tbl_LichTruc obj = new tbl_LichTruc();
            string s = lt.GetMaLichTruc(nv.GetMaKhoa(int.Parse(NVID)), dtpDatlich.Text).ToString();
            if (s == null)
            {
                pnpChiTietLichTruc.Visible = true;

            }
            else
            {
                pnpChiTietLichTruc.Visible = false;
            }


        }

       

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NhanVienBUO nv = new NhanVienBUO();
            LichTrucBUO lt = new LichTrucBUO();
            tbl_LichTruc obj = new tbl_LichTruc();
            int s = int.Parse(lt.GetMaLichTruc(nv.GetMaKhoa(int.Parse(NVID)), dtpDatlich.Text).ToString());
            if (s != -1)
            {
                pnpChiTietLichTruc.Visible = true;
                ShowGrid(s);
                LT = s.ToString();




            }
            else
            {
                MessageBox.Show("Chưa có lịch trực cho ngày " + dtpDatlich.Text + "Vui lòng đặt lịch ");
                pnpChiTietLichTruc.Visible = false;
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                // Only RowHeader and Cells (which make up a row) should fire a menu
                if (hit.Type == DataGridViewHitTestType.RowHeader
                    || hit.Type == DataGridViewHitTestType.Cell
                    && hit.RowIndex >= 0)
                {
                    // Notice how I assign the menu to the dataGridView below...

                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    dataGridView1.ContextMenuStrip = contextMenuStrip1;
                    dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                    ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                }

                // and then I remove the menu here...
                // This is because after we assign it to the dataGridView
                // it allows you to right-click anywhere on the control to
                // get a menu.  Try removing this line and expirement.
                dataGridView1.ContextMenuStrip = null;
            }


        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ChiTietLichTrucBUO chitiet = new ChiTietLichTrucBUO();

            if (chitiet.Delete(int.Parse(ID_Co)))
            {
                MessageBox.Show("Thành công");
            }
            else
            {
                MessageBox.Show("Thất bại");
            }
            ShowGrid(int.Parse(LT));
        }

    }

    }

